package actor.sensori;

import gui.houseeditor.HouseModel;
import gui.houseeditor.HouseModel.HouseObject;
import sde.actor.Message;
import sde.actor.Timer;
import util.Casella;
import attori.controller.ControllerLuceIF;
import attori.controller.ControllerPortaIF;

public class SensorePorta extends Sensore {

	private Casella[][] porta;
	private HouseModel house;

	private static class Timeout extends Message {
	}

	private ControllerPortaIF controller;
	private Timer timer = new Timer();
	private Timeout timeout = new Timeout();
	private long periodo;

	public SensorePorta(HouseModel m, Casella[][] porta,
			ControllerPortaIF controller, long periodo) {
		super();
		this.porta = porta;
		this.controller = controller;
		this.periodo = periodo;
		house = m;
		send(timeout);
	}

	@Override
	public void sample() {
		for (int i = 0; i < porta.length; i++) {
			for (int j = 0; j < porta[0].length; j++) {
				if (house.getCell(porta[i][j].getX(), porta[i][j].getY()) == HouseObject.PERSONA) {
					controller.send(new ControllerPortaIF.Notify());
				}
			}
		}
	}

	@Override
	protected void handler(Message m) {
		if (m == timeout) {
			sample();
			timer.set(m, this, periodo);
		}
	}

}
